/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.content.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.constant.ContentStatic;
import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.dao.content.ContentAttachDao;
import org.grayrabbit.cms.dao.content.SoftwareContentDao;
import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.ContentAttach;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.entity.SoftwareContent;
import org.grayrabbit.cms.service.content.ContentService;
import org.grayrabbit.cms.service.content.SoftwareContentService;
import org.grayrabbit.cms.vo.content.ContentAttachBean;
import org.grayrabbit.cms.vo.content.ContentBean;
import org.grayrabbit.cms.vo.content.SoftwareContentBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>OperLog</b>的service实现类，事务加此处：





 * @version 1.0,创建时间: 2011-05-16 14:40:11  
 */
@Service
@Transactional
public class SoftwareContentServiceImpl extends GenericServiceImpl implements SoftwareContentService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("softwareContentDaoImpl")
	private SoftwareContentDao softwareContentDao;
	
	@Autowired
	@Qualifier("contentAttachDaoImpl")
	private ContentAttachDao contentAttachDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see ContentService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public SoftwareContent getSoftwareContent(final String softwareContentId){
		SoftwareContent softwareContent = softwareContentDao.findById(softwareContentId);
		softwareContentDao.initialize(softwareContent);
		softwareContentDao.initialize(softwareContent.getCategory());
		softwareContentDao.initialize(softwareContent.getContentAttachs());
		softwareContentDao.initialize(softwareContent.getContentTopic());
		softwareContentDao.initialize(softwareContent.getTopic());
		return softwareContent;
	}
	
	/**
	 * @see ContentService#delete(String)
	 */
	@Transactional
	public void delete(final SoftwareContent softwareContent) {
		softwareContentDao.makeTransient(softwareContent);
	}
	
	/**
	 * @see ContentService#save(OperLog)
	 */
	@Transactional
	public void save(final SoftwareContent softwareContent, final Admin user,final String cflag) {
		if(softwareContent!=null){
			if(StringUtils.isBlank(softwareContent.getContentId())){
				if(StringUtils.isNotBlank(user.getLoginName())){
				}
				if(StringUtils.isNotBlank(user.getLoginName())){
					softwareContent.setCreateLoginName(user.getLoginName());
					softwareContent.setUpdateLoginName(user.getLoginName());
				}
				if(StringUtils.isNotBlank(user.getRealName())){
					softwareContent.setCreateRealName(user.getRealName());
					softwareContent.setUpdateRealName(user.getRealName());
				}else{
					softwareContent.setCreateRealName(user.getLoginName());
					softwareContent.setUpdateRealName(user.getLoginName());
				}
				softwareContent.setCreateTime(new Date());
				softwareContent.setUpdateTime(new Date());
				softwareContent.setContentStatus(DictionaryStaticValues.CONTENT_STATUS_NEW);
				softwareContent.setIsLocked(new Boolean(false));
			}else if(DictionaryStaticValues.CONTENT_STATUS_SUCCESS.equals(cflag)
					||DictionaryStaticValues.CONTENT_STATUS_FAIL.equals(cflag)){
				softwareContent.setAuditLoginName(user.getLoginName());
				softwareContent.setAuditRealName(user.getRealName());
				softwareContent.setAuditTime(new Date());
				softwareContent.setUpdateLoginName(user.getLoginName());
				softwareContent.setUpdateRealName(user.getRealName());
				softwareContent.setUpdateTime(new Date()); 
				if(softwareContent.getPublishTime()==null){
					softwareContent.setPublishTime(new Date());
				}
				softwareContent.setContentStatus(cflag);
				softwareContent.setIsLocked(new Boolean(false));
			}else if(DictionaryStaticValues.CONTENT_STATUS_PUBLISH.equals(cflag)){
				softwareContent.setPublishLoginName(user.getLoginName());
				softwareContent.setPublishRealName(user.getRealName());
				if(softwareContent.getPublishTime()==null){
					softwareContent.setPublishTime(new Date());
				}
				softwareContent.setContentStatus(cflag);
				softwareContent.setIsLocked(new Boolean(false));
			}else if(DictionaryStaticValues.CONTENT_STATUS_LOCK.equals(cflag)){
				/*锁定不记录修改信息*/ 
				softwareContent.setIsLocked(new Boolean(true));
			}else{
				if(cflag.equals(DictionaryStaticValues.CONTENT_STATUS_NEW)
						||cflag.equals(DictionaryStaticValues.CONTENT_STATUS_PUBLISH)){
					if(softwareContent.getTemporaryUser()!=null){
						softwareContent.setUpdateRealName(softwareContent.getTemporaryUser());
					}else{
						softwareContent.setUpdateRealName(user.getRealName());
					}
				}else{
					
					softwareContent.setUpdateRealName(user.getRealName());
					softwareContent.setTemporaryUser(user.getRealName());
				}
				softwareContent.setUpdateLoginName(user.getLoginName());
				softwareContent.setUpdateRealName(user.getRealName());
				softwareContent.setUpdateTime(new Date()); 
				softwareContent.setContentStatus(DictionaryStaticValues.CONTENT_STATUS_NEW);
				softwareContent.setIsLocked(new Boolean(false));
			}
		}
		softwareContentDao.makePersistent(softwareContent);		
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see ContentService#getPage(ContentBean)
	 */
	@Transactional(readOnly=true)
	public Page<SoftwareContent> getPage(final SoftwareContentBean softwareContentBean){
		Page<SoftwareContent> page =  new Page<SoftwareContent>();
		page.setTotal(softwareContentDao.count(softwareContentBean));
		page.setResult(softwareContentDao.findByQc(softwareContentDao.assembly(softwareContentBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see ContentService#list(Page, ContentBean)
	 */
	public void list(Page<SoftwareContent> page, SoftwareContentBean softwareContentBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(softwareContentDao.count(softwareContentBean));
		}
		page.setResult(softwareContentDao.findByPage(page,softwareContentBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see ContentService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			ContentAttachBean obj = new ContentAttachBean();
			obj.setContent(softwareContentDao.findById(id));
			List<ContentAttach> list= contentAttachDao.findContentAttch(obj);
			for(int j = 0 ; j < list.size();j++){
				contentAttachDao.makeTransient(list.get(j));
				if(j==MAX_CLEAR)
					contentAttachDao.clear();
			}
			softwareContentDao.makeTransient(softwareContentDao.findById(id));
			i++;
			if(i==MAX_CLEAR)
				softwareContentDao.clear();
			if(ContentStatic.cab!=null){
				ContentStatic.cab.remove(id);
			}
		}
		
	}
	
}